package com.gpt.article.service.impl.taskHandle;

import chatgpt.ChatGPT;
import com.alibaba.fastjson.JSON;
import com.gpt.article.controller.websocket.WebSocketUsers;
import com.gpt.article.domain.AContent;
import com.gpt.article.domain.GptFunPrompts;
import com.gpt.article.domain.GptTopicTitle;
import com.gpt.article.domain.QContent;
import com.gpt.article.service.impl.taskHandle.task.GenQContentTasker;
import com.gpt.article.service.impl.taskHandle.task.XiGaoTasker;
import com.ruoyi.common.config.RuoYiConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenQContentTask extends GenTask{
    private static Logger logger = LoggerFactory.getLogger(GenQContentTask.class);


    private Long taskId;
    private ChatGPT chatGPT;
    private GenTask genTask;

    /**
     * 根据要求生成主题文章内容任务创建
     * @param taskId
     * @param acc
     * @param qContents
     * @return
     */
    public Integer createQContentTask(Long taskId, String acc , List<QContent> qContents,GptFunPrompts gptFunPrompts){
        this.taskId = taskId;
        int genNum = qContents.size();

        //重置完成数量
        new GenQContentTasker().resetExcu();
//        GenQContentTasker.setCompletQContentNum(0);
//        GenQContentTasker.setExcuGenCount(0);
//        GenQContentTasker.setSuccessNum(0);
//        GenQContentTasker.setErrorNum(0);
        try {
            chatGPT = ChatGPT.builder()
                    .apiKey(RuoYiConfig.getApiKeys())
                    .timeout(9000)
//                .proxy(proxy)
                    .apiHost(RuoYiConfig.getGptApiHost()) //代理地址
                    .build()
                    .init();
        }catch (Exception e){
            logger.error("丢包或者请求异常重试:{}",e.getMessage());
            chatGPT = new GptRequstHelper().getGpt(chatGPT);
        }
        int modeNum = 0;
//        List<AContent> aContents = null;
        for(QContent qContent:qContents){
//            aContents = qContent.getaContents();
            String modelCodes = qContent.getModelCodes();
            //转化为数组
            String[] modelCodeArr = modelCodes.split(",");
            modeNum+=modelCodeArr.length;
        }
//        modeNum=genNum*modeNum;
        logger.info("总共需要生成回答:{}",modeNum);

        String  message = "任务提示:\n 当前需生成：prompts "+genNum+"个，需生成的回答"+modeNum+"个\n"
                +"剩余任务：prompts "+(genNum)+"个，剩余回答"+(modeNum)+"个\n"+
                "当前任务：等待模型生成中...";
        Integer percentage = 0;

        Map<String,Object> msgmap = new HashMap<String,Object>();
        msgmap.put("message",message);
        msgmap.put("percentage",percentage);
        msgmap.put("status","start");
        msgmap.put("acontentId",null);
        String  jsonString = JSON.toJSONString(msgmap);
        WebSocketUsers.sendMessageToUsersByText(jsonString);

        Map<String,Object> psMap = new HashMap<String,Object>();
        psMap.put("qaType","0");

        System.out.println("加载任务线程数"+Integer.valueOf(RuoYiConfig.getGenThreadCount()));

        genTask = new GenTask.Builder()
                .setTaskNum(genNum)
                .setModelNum(modeNum)
                .setAcc(acc)
                .setThreadCount(Integer.valueOf(RuoYiConfig.getGenThreadCount()))
                .setGenType("qaContent")
                .addPsMap(psMap)
                .setGptFunPrompts(gptFunPrompts)
//                .setGptFunPrompts(gptFunPrompts)
//                .setRefer(refer)
//                .addArticleList(articles)
                .setchatGpt(chatGPT)
//                .setOneToNum(oneToNum)
//                .setUseModel(ChatCompletion.Model.GPT_3_5_TURBO_0613.getName())
                .addQContentTask(qContents)
//                .setTxtlength(txtlength)
//                .setTuswitch(tuswitch)
//                .setXituSwitch(xituSwitch)
                .build();
        genTask.start(false,"qaContent");
        new XiGaoTasker().upTask(taskId,0,0,0,"1");
//        WebSocketUsers.sendMessageToUsersByText("end");
        return  genNum;
    }
}
